from rest_framework import serializers
from user.models import RoleModel,UserModel, AddressModel


# 用户序列化器
class UserSer(serializers.ModelSerializer):        # 只用于序列化输入 # 去掉两边的空格
    password = serializers.CharField(max_length=256, write_only=True, trim_whitespace=True)
    role = serializers.PrimaryKeyRelatedField(queryset=RoleModel.objects.all())
    role_name = serializers.SerializerMethodField(read_only=True)
    def get_role_name(self, obj):
        return obj.role.name

    class Meta:
        model = UserModel
        fields = ["id","nickname","avatar_url","mobile","password","role","role_name","last_login","date_joined"]
        read_only_fields = ["id","date_joined"]


class AddressSer(serializers.ModelSerializer):
    class Meta:
        model = AddressModel
        fields = '__all__'













